package me.wpf.dao;

import me.wpf.model.QueryEsModel;
import me.wpf.utils.DBHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Description:
 *
 * @author 王朋飞
 * @create 2019/11/7
 */
public class DBDao {
    private final static Logger logger = LoggerFactory.getLogger(DBDao.class);

    /**
     * 查询
     * @param id
     * @return
     */
    public static QueryEsModel selectById(String id) {
        QueryEsModel esModel = null;
        String sql = "SELECT * FROM GF_QUERY_ES where ID=?";
        Connection conn = null;
        PreparedStatement state = null;
        ResultSet rs = null;
        try {
            conn = DBHelper.getConnection();
            state = conn.prepareStatement(sql);
            state.setString(1, id);
            rs = state.executeQuery();
            while (rs.next()) {
                esModel = new QueryEsModel();
                esModel.setId(id);
                esModel.setIndexName(rs.getString("INDEX_NAME"));
                esModel.setSaveDay(rs.getInt("SAVE_DAY"));
                esModel.setNumberOfShards(rs.getInt("NUMBER_OF_SHARDS"));
                esModel.setNumberOfReplicas(rs.getInt("NUMBER_OF_REPLICAS"));
            }
        } catch (Exception e) {
            logger.error("执行sql出错:", e);
            e.printStackTrace();
        } finally {
            DBHelper.closeConncetion(conn, state, rs);
        }
        return esModel;
    }


    /**
     * 更新
     * @param id
     * @param indexName
     * @return
     */
    public static int update(String id, String indexName) {
        int flag = -1;
        String sql = "update GF_QUERY_ES set INDEX_NAME=? where ID=?";
        Connection conn = null;
        PreparedStatement state = null;
        ResultSet rs = null;
        try {
            conn = DBHelper.getConnection();
            state = conn.prepareStatement(sql);
            state.setString(1, indexName);
            state.setString(2, id);
            flag = state.executeUpdate();

        } catch (Exception e) {
            logger.error("执行sql出错:", e);
            e.printStackTrace();
        } finally {
            DBHelper.closeConncetion(conn, state, rs);
        }
        return flag;
    }

}
